<?php

require_once 'common/conn.php';
require_once 'share.action.php';

$result = array();
$db = new DBC();
if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}

if (isset($_GET['nickname'])) {
    $nickname = $_GET['nickname'];
}

if (isset($_GET['sex'])) {
    $sex = $_GET['sex'];
}

if (isset($_GET['email'])) {
    $email = $_GET['email'];
}

if (isset($_GET['password'])) {
    $password = $_GET['password'];
}

$is_exist = false;
$sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "' OR username = '" . $usercode . "'";
$data = $db->fetchData($sql);
if ($data != null && isset($data) && count($data) > 0) {
    $is_exist = true;
}
if (isset($_GET['email']) && isset($_GET['nickname'])) {
    $nickname = $_GET['nickname'];
    $email = $_GET['email'];

    $email_exist = false;
    $nickname_exist = false;
    $sql = "SELECT nickname FROM t_device WHERE nickname = '" . $nickname . "'";
    $data = $db->fetchData($sql);
    if ($data != null && isset($data) && count($data) > 0) {
        $nickname_exist = true;
    }
    $sql = "SELECT email FROM t_device WHERE email = '" . $email . "'";
    $data = $db->fetchData($sql);
    if ($data != null && isset($data) && count($data) > 0) {
        $email_exist = true;
    }
    if ($nickname_exist) {
        $result['errorNo'] = "1";
        $result['errorMsg'] = "昵称 " . $nickname . " 已经被使用。";
    } else if ($email_exist) {
        $result['errorNo'] = "1";
        $result['errorMsg'] = "邮箱 " . $email . " 已经被使用。";
    } else {
        if ($is_exist) {
            $sql = "UPDATE t_device SET sex = " . $sex . ",password = '" . $password . "',email = '" . $email . "',nickname = '" . $nickname . "',createTime = now() WHERE devicecode = '" . $usercode . "'";
            if ($db->execute($sql)) {
                $result['errorNo'] = "0";
            } else {
                $result['errorNo'] = "1";
                $result['errorMsg'] = "注册失败";
            }
        } else {
            $sql = "INSERT INTO t_device(devicecode,sex,password,email,nickname,createTime) VALUES('" . $usercode . "'," . $sex . ",'" . $password . "','" . $email . "','" . $nickname . "',now())";
            $id = $db->insertId($sql);
            if ($id > 0) {
                $share = array();
                $share['usercode'] = $usercode;
                $share['type'] = 1;
                $share['message'] = "我完成了注册任务。";
                updateShare($share);

                $result['errorNo'] = "0";
            } else {
                $result['errorNo'] = "1";
                $result['errorMsg'] = "注册失败";
            }
        }
    }
} else {
    if ($is_exist) {
        $result['errorNo'] = 1;
        $result['errorMsg'] = "该用户已经存在";
    } else {
        $sql = "INSERT INTO t_device(devicecode,createTime) VALUES('" . $usercode . "',now())";
        $id = $db->insertId($sql);
        if ($id > 0) {
            $result['errorNo'] = 0;
        } else {
            $result['errorNo'] = 1;
            $result['errorMsg'] = "注册失败";
        }
    }
}
echo json_encode($result);
?>